package com.gamenew.bluetoothdemo.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;

import com.gamenew.bluetoothdemo.i.ICountDownCallBack;

import java.io.InputStream;


/**
 * Created by Administrator on 2016/9/5 0005.
 */
public class IntelligentUtils {
    private volatile static IntelligentUtils singleton;

    private IntelligentUtils() {
    }

    private CountDownTimer countDownTimer;

    //    public static boolean hasService=false;
    public static IntelligentUtils getInstance() {

        if (singleton == null) {

            synchronized (IntelligentUtils.class) {

                if (singleton == null) {
                    singleton = new IntelligentUtils();
                }
            }
        }
        return singleton;
    }

    //倒计时
    public void countDownTime(Long timer, final ICountDownCallBack iCountDownCallBack) {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        countDownTimer = new CountDownTimer(timer, 1000) {
            @Override
            public void onTick(long l) {
                iCountDownCallBack.onCountDown(l / 1000);
            }

            @Override
            public void onFinish() {
                iCountDownCallBack.onCompleteDown();
            }
        };
        countDownTimer.start();

    }

    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    public static Bitmap getAlphaBitmap(Bitmap mBitmap, int mColor) {
        Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mAlphaBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        Bitmap bitmap = mBitmap.extractAlpha();
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return bitmap;
    }

}
